//
//  StaffLogic.m
//  QRMaster
//
//  Created by myApple on 15/12/21.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "GroupLogic.h"
#import "OperationDataBaseModel.h"

@implementation GroupLogic

-(void)insertGroupData:(GroupModel *)model withCommuntiyId:(NSString *)communityId groupClient_timestamp:(NSString *)groupClient_timestamp
{
    NSMutableDictionary * groupDict = [self modelConvertDict:model];
    [groupDict setValue:communityId forKey:@"communityId"];
    [groupDict setValue:groupClient_timestamp forKey:@"groupClient_timestamp"];
    OperationDataBaseModel * operation = [[OperationDataBaseModel alloc]init];
    [operation insertDataTable:@"groupInfo" withDict:groupDict];
}

/**
 *  模型转化为字典
 *
 *  @param model 组模型
 *
 *  @return 可变字典
 */
-(NSMutableDictionary *)modelConvertDict:(GroupModel *)model
{
    NSMutableDictionary * staffDict = [[NSMutableDictionary alloc]init];
    [staffDict setValue:model.groupId forKey:@"groupId"];
    [staffDict setValue:model.groupName forKey:@"groupName"];
    [staffDict setValue:model.groupManagerName forKey:@"groupManagerMobile"];
    [staffDict setValue:model.groupManagerEmail forKey:@"groupManagerEmail"];
    [staffDict setValue:model.groupManagerName forKey:@"groupManagerName"];
    [staffDict setValue:model.groupBeginTime forKey:@"starttime"];
    [staffDict setValue:model.groupEndTime forKey:@"endtime"];
    return staffDict;
}

/**
 *  模型转化为字典
 *
 *  @param communityId 集群ID
 *
 *  @param block       集群用于接收查询到的数组
 */
-(void)queryGroupInfo:(NSString *)communityId block:(arrBlock)arrBlock
{
    NSMutableArray *groupArray=[[NSMutableArray alloc]init];
    OperationDataBaseModel * operation = [[OperationDataBaseModel alloc]init];
    [operation queryDataFromTable:@"groupInfo" withCondition:[NSString stringWithFormat:@"where communityId = %@",communityId] block:^(FMResultSet *set) {
        while ([set next]) {
            GroupModel *groupModel=[[GroupModel alloc]init];
            groupModel.groupId            = [set stringForColumn:@"groupId"];
            groupModel.groupName          = [set stringForColumn:@"groupName"];
            groupModel.groupManagerName   = [set stringForColumn:@"groupManagerName"];
            groupModel.groupManagerMobile = [set stringForColumn:@"groupManagerMobile"];
            groupModel.groupManagerEmail  = [set stringForColumn:@"groupManagerEmail"];
            groupModel.groupBeginTime     = [set stringForColumn:@"starttime"];
            groupModel.groupEndTime       = [set stringForColumn:@"endtime"];
            groupModel.areaCode           = [set stringForColumn:@"areaCode"];
            [groupArray addObject:groupModel];
        }
        if (arrBlock) {
            arrBlock(groupArray);
        }
    }];
}

/**
 *  模型转化为字典
 *
 *  @param communityId 集群ID
 *
 *  @param groupId     组ID
 */
-(void)deleteGroupInfo:(NSString *)groupId withCommunityId:(NSString *)communityId
{
    OperationDataBaseModel * operation = [[OperationDataBaseModel alloc]init];
    [operation deleteDataFromTable:@"groupInfo" withCondition:[NSString stringWithFormat:@"where groupId in (%@) and communityId = %@",groupId,communityId]];
}

/**
 *  模型转化为字典
 *
 *  @param communityId 集群ID
 *
 *  @param groupId     组ID
 *
 *  @param dic         组需要更新的键值对
 */
-(void)updateGuestInfo:(NSString *)groupId communityId:(NSString *)communityId dic:(NSMutableDictionary *)dic
{
    OperationDataBaseModel * operation = [[OperationDataBaseModel alloc]init];
    [operation updateDataFromTable:@"groupInfo" withCondition:[NSString stringWithFormat:@"where groupId = %@ and communityId = %@",groupId,communityId] withDict:dic];
}

/**
 *  根据集群id删除该集群下组信息
 *
 *  @param communityId 集群ID
 *
 */
-(void)deleteGroupInfo:(NSString *)communityId
{
    OperationDataBaseModel * operation = [[OperationDataBaseModel alloc]init];
    [operation deleteDataFromTableByCommunityId:@"groupInfo" communityId:communityId];
}
@end
